package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppAction;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.utils.SkyConstants;

public class ColorButton extends Button {
    private int bgId;
    private int focusBgId;
    private int strId;
    private AppAction action;

    private int btnPadding = SkyConstants.Div(12);

    public ColorButton(Context context, int strId) {
        this(context, AppAction.ACTION_NULL);
        this.setText(strId);
        this.setPadding(btnPadding, btnPadding, btnPadding, btnPadding);
    }

    public ColorButton(Context context, int strId, int bgId, int focusBgId) {
        this(context, AppAction.ACTION_NULL);
        this.setText(strId);
        this.bgId = bgId;
        this.focusBgId = focusBgId;
        refreshBg();
        this.setPadding(btnPadding, btnPadding, btnPadding, btnPadding);
    }

    public ColorButton(Context context, AppAction action) {
        super(context);
        this.setGravity(Gravity.CENTER);
        this.setSingleLine();
        this.setTextSize(SkyConstants.Dpi(30));
        this.setLayoutParams(new LayoutParams(UICommon.btnWidth, UICommon.btnHeight));
        SkinManager.setTextColor(this, R.color.btnColor);
        setAction(action);
        this.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                refreshBg();
                v.setPadding(btnPadding, btnPadding, btnPadding, btnPadding);
            }
        });
        this.setPadding(btnPadding, btnPadding, btnPadding, btnPadding);
    }

    public void setProperty(String text, int bgId, int focusBgId, boolean enabled) {
        this.action = AppAction.ACTION_NULL;
        this.setText(text);
        this.bgId = bgId;
        this.focusBgId = focusBgId;
        setBtnEnabled(enabled);
    }

    public void setAction(AppAction action) {
        this.action = action;
        switch (action) {
            case ACTION_CANCEL:
                bgId = R.drawable.btn_gray;
                focusBgId = R.drawable.btn_gray_focus;
                strId = R.string.cancel;
                break;
            case ACTION_UNINSTALL:
                bgId = R.drawable.btn_gray;
                focusBgId = R.drawable.btn_gray_focus;
                strId = R.string.uninstall;
                break;
            case ACTION_DOWNLOAD:
                bgId = R.drawable.btn_green;
                focusBgId = R.drawable.btn_green_focus;
                strId = R.string.download;
                break;
            case ACTION_INSTALL:
                bgId = R.drawable.btn_blue;
                focusBgId = R.drawable.btn_blue_focus;
                strId = R.string.install;
                break;
            case ACTION_PAUSE:
                bgId = R.drawable.btn_green;
                focusBgId = R.drawable.btn_green_focus;
                strId = R.string.pause;
                break;
            case ACTION_CONTINUE:
                bgId = R.drawable.btn_green;
                focusBgId = R.drawable.btn_green_focus;
                strId = R.string.continue_download;
                break;
            case ACTION_LAUNCH:
                bgId = R.drawable.btn_green;
                focusBgId = R.drawable.btn_green_focus;
                strId = R.string.launch;
                break;
            case ACTION_UPGRADE:
                bgId = R.drawable.btn_orange;
                focusBgId = R.drawable.btn_orange_focus;
                strId = R.string.upgrade;
                break;
            case ACTION_DELETE:
                bgId = R.drawable.btn_gray;
                focusBgId = R.drawable.btn_gray_focus;
                strId = R.string.delete;
                break;
            case ACTION_NULL:
                bgId = R.drawable.btn_green;
                focusBgId = R.drawable.btn_green_focus;
                strId = 0;
                break;
            case ACTION_PRIORITIZE:
                bgId = R.drawable.btn_green;
                focusBgId = R.drawable.btn_green_focus;
                strId = R.string.prioritize;
                break;
        }
        if (strId != 0)
            this.setText(strId);
        refreshBg();
    }

    private void refreshBg() {
        if (this.hasFocus())
            SkinManager.setBackground(this, focusBgId);
        else
            SkinManager.setBackground(this, bgId);
    }

    public void setBtnEnabled(boolean enabled) {
        if (enabled)
            this.setAlpha(1.0f);
        else
            this.setAlpha(UICommon.disableAlpha);
        this.setEnabled(enabled);
    }

    public AppAction getAction() {
        return action;
    }
}
